package cn.wolfcode.car.controller;

import cn.wolfcode.car.ann.RequiredPermission;
import cn.wolfcode.car.domain.Department;
import cn.wolfcode.car.qo.DepartmentQueryObject;
import cn.wolfcode.car.service.IDepartmentService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import result.JsonResult;

import java.util.List;

@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;



    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") DepartmentQueryObject qo) {
        PageInfo<Department> list = departmentService.list(qo);
        System.out.println(list);
        model.addAttribute("result", list);
        List<Department> departments = departmentService.listAll();
        model.addAttribute("departments", departments);
        return "department/list";
    }


    @PostMapping("/saveOrUpdate")
    public String saveOrUpdate(Department department) {
        if (department.getId() == null) {
            departmentService.save(department);
        } else {
            departmentService.update(department);
        }
        return "redirect:/department/list";
    }


    @GetMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        departmentService.delete(id);
        return JsonResult.success(null);
    }
}
